from django.http import JsonResponse
from django.shortcuts import render, HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django_redis import get_redis_connection

from test01.modelform import UserInfoModelForm
from utils.tencent.sms import *
# import redis


# Create your views here.

def sms_test(request):
    """短信发送测试"""
    # return_msg = send_sms_single('1802070101', 1757692, [12345, 5])
    # print(return_msg)
    # if return_msg:
    #     return HttpResponse(return_msg.get('errmsg'))
    return HttpResponse('发送成功！')


def register(request):
    """ 注册 """
    form = UserInfoModelForm()
    return render(request, 'test01/register.html', {'form': form})


@csrf_exempt
def get_code(request):
    """ 获取验证码 """
    # conn = redis.Redis(host='127.0.0.1', port=6379, password='1212', encoding='utf-8')
    # conn.set('18020701010', 99999, ex=10)
    conn = get_redis_connection('default')
    conn.set('18020701010', 8888, ex=10)
    context = {
        'status': True,
        'data': conn.get('18020701010').decode(encoding='utf-8'),
    }
    return JsonResponse(context)
